<?php
class ModelCommunityForumSetting extends Model {
    public function getForumSetting($group, $store_id = 0) {
        $data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '" . (int)$store_id . "' AND `group` = '" . $this->db->escape($group) . "'");

        foreach ($query->rows as $result) {
            if (!$result['serialized']) {
                $data[$result['key']] = $result['value'];
            } else {
                $data[$result['key']] = unserialize($result['value']);
            }
        }

        return $data;
    }

    public function getForums() {
        $query = $this->db->query("SELECT f.forum_id, n.forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id<>'0' ORDER BY f.sort_order_id ASC, n.forum_name ASC");

        return $query->rows;
    }

    public function getSubForums($parent_forum_id) {
        $query = $this->db->query("SELECT f.forum_id, n.forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.parent_id='" . (int)$parent_forum_id . "' ORDER BY f.sort_order_id ASC, n.forum_name ASC");

        return $query->rows;
    }

    public function getYourDayForums() {
        $query = $this->db->query("SELECT f.forum_id, n.forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id<>'0' ORDER BY f.sort_order_id ASC, n.forum_name ASC");

        return $query->rows;
    }

    public function editForumSetting($group, $data, $store_id = 0) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "setting WHERE store_id = '" . (int)$store_id . "' AND `group` = '" . $this->db->escape($group) . "'");

        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `group` = '" . $this->db->escape($group) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'");
            } else {
                $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `group` = '" . $this->db->escape($group) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(serialize($value)) . "', serialized = '1'");
            }
        }
    }
}
?>